#include "stdafx.h"
#include "gamewizard.h"
#include "appsettings.h"

GameWizard::GameWizard( QWidget * parent )
:   DialogBase( parent ),
    m_topicCategory( 0 )
{
    connect( ui.bibleCheckBox, SIGNAL( clicked( bool ) ), this, SLOT( onBibleCheckBoxToStateChanged( bool ) ) );
    connect( ui.teoCheckBox, SIGNAL( clicked( bool ) ), this, SLOT( onTeoCheckBoxStateChanged( bool ) ) );
    connect( ui.scientificCheckBox, SIGNAL( clicked( bool ) ), this, SLOT( onScientificCheckBoxStateChanged( bool ) ) );
    connect( ui.humanCheckBox, SIGNAL( clicked( bool ) ), this, SLOT( onHumanCheckBoxStateChanged( bool ) ) );
    connect( ui.valusListWidget, SIGNAL( itemClicked( QListWidgetItem * ) ), this, SLOT( onValusListWidgetItemClicked( QListWidgetItem * ) ) );
    connect( ui.okPushButton, SIGNAL( clicked() ), this, SLOT( onOk() ) );
    connect( ui.cancelPushButton, SIGNAL( clicked() ), this, SLOT( reject() ) );

    for (int i = 1; i <= 5; i ++)
    {
        QListWidgetItem * item = new QListWidgetItem( QString( "%1" ).arg( i ) );
        item->setData( Qt::UserRole + 1, QVariant( i ) );
        item->setCheckState( Qt::Unchecked );
        ui.valusListWidget->addItem( item );
    }

    QVariant variant = AppSettings::value( "TopicCategory" );
    if (variant.isValid())
    {
        int value = variant.toInt();
        if (isFlag( value, TopicCategory::Bible))
        {
            setFlag( m_topicCategory, TopicCategory::Bible );
            ui.bibleCheckBox->setChecked( true );
        }

        if (isFlag( value, TopicCategory::Teo))
        {
            setFlag( m_topicCategory, TopicCategory::Teo );
            ui.teoCheckBox->setChecked( true );
        }

        if (isFlag( value, TopicCategory::Scientific))
        {
            setFlag( m_topicCategory, TopicCategory::Scientific );
            ui.scientificCheckBox->setChecked( true );
        }

        if (isFlag( value, TopicCategory::Humanitarian))
        {
            setFlag( m_topicCategory, TopicCategory::Humanitarian );
            ui.humanCheckBox->setChecked( true );
        }

    }
    
    variant = AppSettings::value( "TopicCount" );
    if (variant.isValid())
    {
        int value = variant.toInt();
        if (value > 0 && value <= 10)
        {
            ui.topicCountSpinBox->setValue( value );
        }
    }

    variant = AppSettings::value( "PlayersCount" );
    if (variant.isValid())
    {
        int value = variant.toInt();
        if (value == 2 || value == 3)
        {
            ui.playersCountSpinBox->setValue( value );
        }
    }

    variant = AppSettings::value( "Values" );
    if (variant.isValid())
    {
        QByteArray value = variant.toByteArray();
        if (( value.count() % 4 ) != 0)
            return;
        
        int count = value.count() / 4;
        for (int i = 0; i < count; i ++)
        {
            int val = (int) * ( value.data() + ( 4 * i ) );
            if (val < 1 || val > 5)
                return;

            m_values.insert( val );
            for (int j = 0; j < ui.valusListWidget->count(); j ++)
            {
                QListWidgetItem * item = ui.valusListWidget->item( j );
                if (item->data( Qt::UserRole + 1 ).toInt() == val )
                {
                    item->setCheckState( Qt::Checked );
                    break;
                }
            }   
        } 
    }

    enableSavePushButton();
}

GameWizard::~GameWizard()
{
}

void GameWizard::data( int & topicCategory, int & topicCount, QVector<int> & values, int & playersCount ) const
{
    topicCategory = m_topicCategory;
    topicCount = ui.topicCountSpinBox->value();
    playersCount = ui.playersCountSpinBox->value();
    QSet<int>::const_iterator it;
    FOREACH (it, m_values)
        values.push_back( * it );
}

void GameWizard::enableTopicCategory( TopicCategory::Enum topicCategory, bool enable )
{
     if (enable)
        setFlag( m_topicCategory, topicCategory );
    else
        clearFlag( m_topicCategory, topicCategory );
}

void GameWizard::enableSavePushButton()
{
    bool saveEnabled = ( m_topicCategory != 0 && ! m_values.isEmpty() );
    ui.okPushButton->setEnabled( saveEnabled );
}
void GameWizard::onBibleCheckBoxToStateChanged( bool checked )
{
    enableTopicCategory( TopicCategory::Bible, checked );
    enableSavePushButton();
}

void GameWizard::onTeoCheckBoxStateChanged( bool checked )
{
    enableTopicCategory( TopicCategory::Teo, checked );
    enableSavePushButton();
}

void GameWizard::onScientificCheckBoxStateChanged( bool checked )
{
    enableTopicCategory( TopicCategory::Scientific, checked );
    enableSavePushButton();
}

void GameWizard::onHumanCheckBoxStateChanged( bool checked )
{
    enableTopicCategory( TopicCategory::Humanitarian, checked );
    enableSavePushButton();
}

void GameWizard::onValusListWidgetItemClicked( QListWidgetItem * item )
{
    int data = item->data( Qt::UserRole + 1 ).toInt();

    if (item->checkState() == Qt::Checked)
        m_values.insert( data );
    else
        m_values.remove( data );

    enableSavePushButton();
}

void GameWizard::onOk()
{
    AppSettings::setValue( "TopicCategory", m_topicCategory );
    AppSettings::setValue( "TopicCount", ui.topicCountSpinBox->value() );
    AppSettings::setValue( "PlayersCount", ui.playersCountSpinBox->value() );

    QByteArray values;
    QSet<int>::const_iterator it;
    FOREACH (it, m_values)
    {
        int value = * it;
        values.append( (const char *) & value , sizeof( int ) );
    }

    AppSettings::setValue( "Values", QVariant( values ) );
    QDialog::accept();
}